﻿using Architecture.Repository.Impl.Traceability;
using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.SupplierDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.AuxiliaryMaterialsServices
{
    /// <summary>
    /// 辅料服务
    /// </summary>
    public class AuxiliaryMaterialsService : IAuxiliaryMaterialsService
    {
        /// <summary>
        /// 包材仓储
        /// </summary>
        protected readonly IAuxiliaryMaterialsRepository auxiliaryMaterialsRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="auxiliaryMaterialsRepository">包材仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public AuxiliaryMaterialsService(IAuxiliaryMaterialsRepository auxiliaryMaterialsRepository, IPictureRepository pictureRepository)
        {
            this.auxiliaryMaterialsRepository = auxiliaryMaterialsRepository;
            this.pictureRepository = pictureRepository;
        }

        public async Task<int> AddAuxiliaryMaterials(AuxiliaryMaterials auxiliaryMaterials)
        {
            return await auxiliaryMaterialsRepository.AddAsync(auxiliaryMaterials);
        }

        public async Task<int> DeleteAuxiliaryMaterials(int auxiliaryMaterialsId, string userMame)
        {
            var res = await auxiliaryMaterialsRepository.GetModelAsync(auxiliaryMaterialsId);
            res.UpdatedBy = userMame;
            res.UpdatedDate = DateTime.Now;
            return await auxiliaryMaterialsRepository.UpdateAsync(res);
        }

        public async Task<List<AuxiliaryMaterialsDto>> GetAllAuxiliaryMaterialsList(int shoppingId)
        {
            var list = (from a in await auxiliaryMaterialsRepository.GetAllAsync()
                        join b in await pictureRepository.GetAllAsync()
                        on a.AuxiliaryMaterialsId equals b.AuxiliaryMaterialsId
                        where a.ShoppingId == shoppingId && !a.IsDeleted && !b.IsDeleted
                        select new AuxiliaryMaterialsDto
                        {
                            AuxiliaryMaterialsId = a.AuxiliaryMaterialsId,
                            AuxiliaryMaterialsProductionBatch = a.AuxiliaryMaterialsProductionBatch,
                            ShoppingId=a.ShoppingId,
                            PictureId = b.PictureId,
                            PictureName = b.PictureName,
                        }).ToList();
            return list;
        }

        public async Task<AuxiliaryMaterials> GetAuxiliaryMaterialsModel(int auxiliaryMaterialsId)
        {
            return await auxiliaryMaterialsRepository.GetModelAsync(auxiliaryMaterialsId);
        }

        public async Task<int> UpdateAuxiliaryMaterials(AuxiliaryMaterials auxiliaryMaterials)
        {
            var res = await auxiliaryMaterialsRepository.GetModelAsync(auxiliaryMaterials.AuxiliaryMaterialsId);
            res.AuxiliaryMaterialsId = auxiliaryMaterials.AuxiliaryMaterialsId;
            res.AuxiliaryMaterialsProductionBatch = auxiliaryMaterials.AuxiliaryMaterialsProductionBatch;
            res.UpdatedBy = auxiliaryMaterials.UpdatedBy;
            res.UpdatedDate = DateTime.Now;
            return await auxiliaryMaterialsRepository.UpdateAsync(res);
        }
    }
}
